package com.tanhua.service;

import com.tanhua.domain.Question;
import com.tanhua.domain.Settings;
import com.tanhua.dubbo.QuestionAPI;
import com.tanhua.dubbo.SettingsAPI;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @DubboReference
    private QuestionAPI questionAPI;

    @DubboReference
    private SettingsAPI settingsAPI;

    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1 获取id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        vo.setPhone(UserHolder.getUserMobile());
        //2 陌生人问题
        Question question = questionAPI.findByUserId(userId);
        if (question==null){
            vo.setStrangerQuestion("你喜欢士博吗?");
        }else{
            vo.setStrangerQuestion(question.getTxt());
        }
        //3 各种通知开关
        Settings settings = settingsAPI.fingByUserId(userId);
        if (settings!=null){
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }

        return vo;
    }

    public void saveOrUpdate(Settings settings) {
        settingsAPI.saveOrUpdate(UserHolder.getUserId(),settings);
    }
}
